<template>
  <el-container style="min-height: 100vh">

    <el-aside :width="sideWidth + 'px'" style="box-shadow: 2px 0 6px rgb(0 21 41);">
      <el-menu :default-openeds="['1', '3']" style="min-height: 100%; overflow-x: hidden"
               background-color="rgb(48, 65, 86)"
               text-color="#fff"
               active-text-color="#ffd04b"
               :collapse-transition="false"
               :collapse="isCollapse"
      >
        <div style="height: 60px; line-height: 60px; text-align: center">
          <img src="@/assets/logo.png" alt="" style="width: 20px; position: relative; top: 5px; right: 5px">
          <b style="color: white" v-show="logoTextShow">后台管理系统</b>
        </div>
        <el-submenu index="1">
          <template slot="title">
            <i class="el-icon-message"></i>
            <span slot="title">后台主页</span>
          </template>
          <!-- <el-menu-item index="1-0" ><router-link class="routerto" to="/">后台管理主页</router-link></el-menu-item> -->
          <el-menu-item index="1-1" ><router-link class="routerto" to="/goodsManage">商品后台管理</router-link></el-menu-item>
          <el-menu-item index="1-2"><router-link class="routerto" to="/user">用户后台管理</router-link></el-menu-item>
          <el-menu-item index="1-2"><router-link class="routerto" to="/order">订单后台管理</router-link></el-menu-item>
        </el-submenu>
      </el-menu>
    </el-aside>

    <el-container>
      <el-header style="font-size: 12px; border-bottom: 1px solid #ccc; line-height: 60px; display: flex">
        <div style="flex: 1; font-size: 20px">
          <span :class="collapseBtnClass" style="cursor: pointer" @click="collapse"></span>
        </div>
        <el-dropdown style="width: 70px; cursor: pointer">
          <span>ADRIAN</span>
        </el-dropdown>

      </el-header>

      <el-main>
        <div style="margin-bottom: 30px">
          <el-breadcrumb separator="/">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>订单管理</el-breadcrumb-item>
          </el-breadcrumb>
        </div>

        <div style="margin: 10px 0">
          <el-input style="width: 200px" placeholder="请输入用户的编号" suffix-icon="el-icon-search" v-model="ouId"></el-input>
          <el-input style="width: 200px" placeholder="订单金额大于" suffix-icon="el-icon-position" class="ml-5" v-model="orderPrice"></el-input>
          <el-button class="ml-5" type="primary" @click="load">搜索</el-button>
          <el-button type="warning" @click="reset">重置</el-button>
        </div>



        <el-table :data="tableData" border stripe :header-cell-class-name="headerBg"  @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55"></el-table-column>
          <el-table-column prop="orderId" label="订单编号" width="80"></el-table-column>
          <el-table-column prop="orderPrice" label="订单金额" width="80"></el-table-column>
          <el-table-column prop="orderStatus" label="订单状态" width="140"></el-table-column>
          <el-table-column prop="ouId" label="用户编号" width="120"></el-table-column>
          <el-table-column prop="createTime" label="订单创建时间"></el-table-column>
          <el-table-column prop="updateTime" label="订单最近修改时间"></el-table-column>
          <el-table-column label="操作"  width="200" align="center">
            <template slot-scope="scope">
                   <el-button type="primary" slot="reference" @click="doActive(scope.row.orderId)">订单进行 <i class="el-icon-remove-outline"></i></el-button>
              <el-button type="primary" slot="reference" @click="doFreeze(scope.row.orderId)">冻结 <i class="el-icon-remove-outline"></i></el-button>
              <el-button type="primary" slot="reference" @click="done(scope.row.orderId)">订单完成 <i class="el-icon-remove-outline"></i></el-button>
              <el-popconfirm
                  class="ml-5"
                  confirm-button-text='给我删！'
                  cancel-button-text='再让我想想呗'
                  icon="el-icon-info"
                  icon-color="red"
                  title="您确定删除吗？"
                  @confirm="del(scope.row.orderId)"
              >
                <el-button type="danger" slot="reference">删除 <i class="el-icon-remove-outline"></i></el-button>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
        <div style="padding: 10px 0">
          <el-pagination
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              :current-page="pageNum"
              :page-sizes="[3, 5, 10, 20]"
              :page-size="pageSize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="total">
          </el-pagination>
        </div>
      </el-main>

    </el-container>
  </el-container>
</template>

<script>

// import request from "@/utils/request";
import router from "../../router";
export default {
  name: 'ManageOrder',
  data() {
    return {
      tableData: [],
      total: 0,
      pageNum: 1,
      pageSize: 3,
      orderPrice: "",
      ouId: "",
      orderId:"",

      form: {},
      multipleSelection: [],
      msg: "hello thh",
      collapseBtnClass: 'el-icon-s-fold',
      isCollapse: false,
      sideWidth: 200,
      logoTextShow: true,
      headerBg: 'headerBg'
    }
  },
  created() {
    // 请求分页查询数据
    this.load()
  }, mounted() {
    this.cheackAdmin();
  },
  methods: {

     cheackAdmin() {
      this.$axios
        .get("/admin/index", {
          headers: {
            "x-access-token": localStorage.getItem("x-access-token"),
          },
        })
        .then((res) => {
          console.log(res);
          if (res.data.code == 401 && "用户不存在！" == res.data.extend.result) {
            this.$message.error("请先登录再访问后台");
            router.push("/adminpage/login");
          } else if (
            res.data.code == 401 &&
            "登录失败，该用户没有权限" == res.data.extend.result
          ) {
            this.$message.error("登录失败，该用户不是管理员");
              router.push("/adminpage/login");
          }else if (
            res.data.code == 401 &&
            "该用户已被锁定,暂时无法登录！" == res.data.extend.result
          ) {
            this.$message.error("登录失败，该用户不是管理员");
                router.push("/adminpage/login");
          }
          
          else if (
            res.data.code == 200 &&
            "登陆成功" == res.data.extend.result
          ) {
            this.$message({
              message: "管理员你好，欢迎来到后台管理页面",
              type: "success",
            });
            
          }
        });
    },

    collapse() {  // 点击收缩按钮触发
      this.isCollapse = !this.isCollapse
      if (this.isCollapse) {  // 收缩
        this.sideWidth = 64
        this.collapseBtnClass = 'el-icon-s-unfold'
        this.logoTextShow = false
      } else {   // 展开
        this.sideWidth = 200
        this.collapseBtnClass = 'el-icon-s-fold'
        this.logoTextShow = true
      }
    },
    load() {
      this.request.get("/manage/orderPage", {
        params: {
          pageNum: this.pageNum,
          pageSize: this.pageSize,
          orderPrice: this.orderPrice,
          ouId: this.ouId,
        }
      }).then(res => {
        this.tableData = res.records
        this.total = res.total
      })
    },
    del(orderId) {
      this.request.delete("/manage/deleteOrder/" + orderId).then(res => {
        if (res) {
          this.$message.success("删除成功")
          this.load()
        } else {
          this.$message.error("删除失败")
        }
      })
    },
    reset() {
      this.orderPrice=""
      this.ouId=""
      this.load()
    },
    handleSizeChange(pageSize) {
      console.log(pageSize)
      this.pageSize = pageSize
      this.load()
    },
    handleCurrentChange(pageNum) {
      console.log(pageNum)
      this.pageNum = pageNum
      this.load()
    },

    //激活用户
    doActive(tOrderId){
      this.request.get("/manage/activateOrder", {
        params: {
          orderId: tOrderId
        }
      }).then(res => {
        if (res) {
          this.$message.success("订单激活成功")
          this.load()
        } else {
          this.$message.error("订单激活失败")
          this.load()
        }
      })
    },
    doFreeze(tOrderId){
      this.request.get("/manage/freezeOrder", {
        params: {
          orderId: tOrderId
        }
      }).then(res => {
        if (res) {
          this.$message.success("订单冻结成功")
          this.load()
        } else {
          this.$message.error("订单冻结失败")
          this.load()
        }
      })
    },
    done(tOrderId){
      this.request.get("/manage/doneOrder", {
        params: {
          orderId: tOrderId
        }
      }).then(res => {
        if (res) {
          this.$message.success("订单完成")
          this.load()
        } else {
          this.$message.error("订单完成")
          this.load()
        }
      })
    },

  }
}
</script>

<style>
.headerBg {
  background: #eee!important;
}
.routerto{
  color: #fff;
  text-decoration: none;

}
</style>
